import { ObjectWrap } from "../../../../Core/FrameEx/ES5Ex";
import { ITEM_TYPE, PART_ITEM } from "../../../Common/Define";
import GameMgr from "../../../Logic/GameMgr";
import { KF } from './../../../Common/Define';
import { REquireItem } from './RItemAttach';

export default class RJXItem extends ObjectWrap {
    /**ID */
    public id: number = null;
    /**类型 */
    public type: number = null;
    /**数量 */
    public num: number = null;
    /**key */
    public key: string = null;
    /**数据 */
    public raw: SItemRaw;

    public attach: REquireItem;

    public get icon(): string {
        return "icon_" + this.raw.icon;
    }
    public static _tpl: RJXItem;
    public static tpl(...args: any[]): RJXItem {
        if (!this._tpl) this._tpl = new RJXItem();
        if (!args || args.length == 0) return this._tpl;
        this._tpl.init(...args);
        return this._tpl;
    }

    constructor(item?: any[]) {
        super();
        if (item) {
            this.init(...item);
        }
    }



    /** 物品名称 */
    public get name(): string {
        if (!this.raw) {
            console.log("can't find item:", this.id, this.type);
            return;
        }

        if ((CC_DEBUG || CC_DEV) && window['WD_DEBUG']) {
            return this.raw.name + JSON.stringify([this.id, this.type, this.num])
        }
        else {
            return this.raw.name;
        }
    }

    protected instantiate(attach: any) {
        switch (this.type) {
            /** 1-装备 */
            case ITEM_TYPE.EQUIPDATA: {
                if (!attach) {
                    // cc.warn('the equire un instantiate infomations!');
                    return;
                }
                if (attach instanceof REquireItem) {
                    this.attach = attach as any;
                    return;
                }
                this.attach = new REquireItem(attach, this) as any;
                break;
            };
        }
    }

    /** 最大叠堆数量 */
    public get maxPile(): number {
        if (!this.raw) return 1;
        if (this.raw.maxPile)
            return this.raw.maxPile;
        // cc.warn(`this item is not maxpile: ${this.key}`);
        return 1;
    }


    public get quality(): number {
        switch (this.type) {
            case ITEM_TYPE.EQUIPDATA:
                {
                    if (this.attach) {
                        return this.attach.quality;
                    } else {
                        return this.raw.trait
                    }
                }
            case ITEM_TYPE.HERO:
                {
                    if (this.attach) {
                        return this.attach.quality;
                    } else {
                        return this.raw.trait
                    }
                }
            case ITEM_TYPE.PROP:
                {
                    if (this.attach) {
                        return this.attach.quality;
                    } else {
                        return this.raw.trait
                    }
                }
            default:
                {
                    return this.raw.quality;
                }
        }
    }

    private init(...args: any): RJXItem {
        this.id = args[PART_ITEM.ID];
        this.type = args[PART_ITEM.TYPE];
        this.num = args[PART_ITEM.NUM];
        this.key = KF.genItemKey(this.id, this.type);
        this.parseItem();
        this.instantiate(args[3]);
        return this;
    }


    public changeNum(v: number) {
        this.num += v;
    }

    protected parseItem() {
        switch (this.type) {
            case ITEM_TYPE.HERO:
                break;
            case ITEM_TYPE.EQUIPDATA:
                this.raw = GameMgr.equipData.getRaw(this.id);
                break;
            case ITEM_TYPE.CURRENCY:
            case ITEM_TYPE.PROP: {
                this.raw = GameMgr.itemData.getRaw(this.id);
                break;
            }
            default:
                break;
        }
    }
}